﻿//********************************************************************************//
//创建日期：<创建日期，2016-3-22>
//创建作者：<孙雪婷，sunxueting@shanghai3h.com>
//功能说明：拟定计划查错
//********************************************************************************//
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Entity;
using BLL;
using BLL.Access;
using System.Data;
using Platform.Http;
using Web.Class;
using DevExpress.Web;

namespace Web.Pages.YongShui.JiHuaGL
{
    public partial class NiDingJHCC : BasePage
    {
        #region 属性

        

        /// <summary>
        /// 当前操作人站点
        /// </summary>
        private string ZhanDianBH
        {
            get
            {
                return Convert.ToString(ViewState["_ZhanDianBH"]);
            }
            set
            {
                ViewState["_ZhanDianBH"] = value;
            }
        }

        /// <summary>
        /// 水量年
        /// </summary>
        private int ShuiLiangN
        {
            get
            {
                return Convert.ToInt32(ViewState["_ShuiLiangN"]);
            }
            set
            {
                ViewState["_ShuiLiangN"] = value;
            }
        }

        /// <summary>
        /// 水量月
        /// </summary>
        private int ShuiLiangM
        {
            get
            {
                return Convert.ToInt32(ViewState["_ShuiLiangM"]);
            }
            set
            {
                ViewState["_ShuiLiangM"] = value;
            }
        }

        /// <summary>
        /// 保存结果集1
        /// </summary>
        private DataTable dtCuoWuXX
        {
            get
            {
                return ViewState["dtCuoWuXX"] as DataTable;
            }
            set
            {
                ViewState["dtCuoWuXX"] = value;
            }
        }

       
        #endregion

        #region 变量

        /// <summary>
        /// 拟定计划错误信息
        /// </summary>
        YS_JH_NIDINGCWXX _YS_JH_NIDINGCWXX = new YS_JH_NIDINGCWXX();
        
        /// <summary>
        /// 拟定计划错误信息BLL
        /// </summary>  
        YS_JH_NIDINGCWXXBLL _YS_JH_NIDINGCWXXBLL = new YS_JH_NIDINGCWXXBLL();

        /// <summary>
        /// 确定计划
        /// </summary>
        YS_JH_QUEDINGJH _YS_JH_QUEDINGJH = new YS_JH_QUEDINGJH();

        /// <summary>
        /// 确定计划BLL
        /// </summary>  
        YS_JH_QUEDINGJHBLL _YS_JH_QUEDINGJHBLL = new YS_JH_QUEDINGJHBLL();

        /// <summary>
        /// 计划用水
        /// </summary>  
        YS_JH_YUANJIHUA _YS_JH_YUANJIHUA = new YS_JH_YUANJIHUA();

        /// <summary>
        /// 计划用水BLL
        /// </summary>  
        YS_JH_YUANJIHUABLL _YS_JH_YUANJIHUABLL = new YS_JH_YUANJIHUABLL();

        /// <summary>
        /// 用户信息对象
        /// </summary>
        YS_YG_YONGHUXX _YS_YG_YONGHUXX = new YS_YG_YONGHUXX();

        /// <summary>
        /// 用户信息BLL
        /// </summary>  
        YS_YG_YONGHUXXBLL _YS_YG_YONGHUXXBLL = new YS_YG_YONGHUXXBLL();

        /// <summary>
        /// 公司信息
        /// </summary>
        YS_JX_GONGSIXX _YS_JX_GONGSIXX = new YS_JX_GONGSIXX();

        /// <summary>
        /// 公司信息bll
        /// </summary>
        YS_JX_GONGSIXXBLL _YS_JX_GONGSIXXBLL = new YS_JX_GONGSIXXBLL();

        /// <summary>
        /// 局信息
        /// </summary>
        YS_JX_JUXINX _YS_JX_JUXINX = new YS_JX_JUXINX();

        /// <summary>
        /// 局信息bll
        /// </summary>
        YS_JX_JUXINXBLL _YS_JX_JUXINXBLL = new YS_JX_JUXINXBLL();

        /// <summary>
        /// 操作日志BLL
        /// </summary>
        SYS_LOGBLL _SYS_LOGBLL = new SYS_LOGBLL();

       
        /// <summary>
        /// 存储过程调用类
        /// </summary>
        SpHelper _SpHelper = new SpHelper();

        /// <summary>
        /// SQL调用类
        /// </summary>
        BLL.SqlHelper _SqlHelper = new BLL.SqlHelper();

        #endregion

        #region 事件

        /// <summary>
        /// 页面加载事件
        /// </summary>
        protected void Page_Load(object sender, EventArgs e)
        {
            //第一次页面加载
            if (!IsPostBack)
            {
                LoadData();
                Display();
            }
            gvChaCuoJC.DataSource = this.dtCuoWuXX;
            gvChaCuoJC.DataBind();
           
        }


    

        /// <summary>
        /// 放弃修改
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnCancel_Click(object sender, EventArgs e)
        {
            //GridViewContentTable.Visible = false;
           // this.gvXiangXiXX.Enabled = true;
            //this.Status = GridViewStatus.Init;
            //this.btnliulan.Visible = true;
            //Display();
        }


        /// <summary>
        /// 前台回调
        /// </summary>
        /// <param name="source"></param>
        /// <param name="e"></param>
        protected void ASPxCallback1_Callback(object source, DevExpress.Web.CallbackEventArgs e)
        {
            string msg = "";
            Func<bool> getJHInfo = () =>
            {
                string yhh = this.hdCollection.Get("yhh").ToString();
                DataTable YONGHUXX = _YS_YG_YONGHUXXBLL.SelectYS_YG_YONGHUXXesDynamic(string.Format("S_YONGHUH='{0}' AND I_JLZT=0 ", yhh), null);
                if (YONGHUXX.Rows.Count == 0)
                {
                    msg = string.Join("|", "false", "不存在该用户信息。请检查该用户是否已销户（或并户、分户）。");
                    return false;
                }
                string yonghum = YONGHUXX.Rows.Count > 0 ? YONGHUXX.Rows[0]["S_YONGHUM"].ToString() : "";
                DataTable dt = _YS_JH_QUEDINGJHBLL.SelectYS_JH_QUEDINGJHsDynamic(string.Format(" I_JLZT=0 AND I_TYPE=3 AND S_YONGHUH='{0}'", yhh), null);
                string yonghuh = dt.Rows[0]["S_YONGHUH"].ToString();
                string juhao = dt.Rows[0]["S_JUBIANH"].ToString();
                string gongsi = dt.Rows[0]["S_GONGSIBH"].ToString();
                string juhao2 = "", gongsi2 = "";
                if (juhao != "")
                {
                    DataTable GongSiXX = _YS_JX_GONGSIXXBLL.SelectYS_JX_GONGSIXXesDynamic(string.Format("s_bianhao='{0}' and i_leixing=0", txtGongSiM.Text), null);
                    if (GongSiXX.Rows.Count > 0) juhao2 = GongSiXX.Rows[0][2].ToString();
                }
                if (gongsi != "")
                {
                    DataTable JuXinX = _YS_JX_JUXINXBLL.SelectYS_JX_JUXINXesDynamic(string.Format("s_bianhao='{0}' and i_leixing=0", txtJuHao.Text), null);
                    if (JuXinX.Rows.Count > 0) gongsi2 = JuXinX.Rows[0][3].ToString();
                }
                msg = string.Join("|", yonghuh, yonghum, juhao, juhao2, gongsi, gongsi2, dt.Rows[0]["I_M1"].ToString(), dt.Rows[0]["I_M2"].ToString(),
                    dt.Rows[0]["I_M3"].ToString(), dt.Rows[0]["I_M4"].ToString(), dt.Rows[0]["I_M5"].ToString(), dt.Rows[0]["I_M6"].ToString(),
                    dt.Rows[0]["I_M7"].ToString(), dt.Rows[0]["I_M8"].ToString(), dt.Rows[0]["I_M9"].ToString(), dt.Rows[0]["I_M10"].ToString(),
                    dt.Rows[0]["I_M11"].ToString(), dt.Rows[0]["I_M12"].ToString());
                return true;
            };
            if (e.Parameter == "save")
            {
                bool result = SaveData();
                if (result)
                {
                    msg = "操作成功！";
                }
            }
            else if (e.Parameter == "CheckVlidate")
            {
                DataTable dt_n = _SpHelper.P_YSJH_XIADAJH_N().Tables[0];
                string result = dt_n.Rows.Count > 0 && dt_n.Rows[0][0].ToString() == "1" ? "" : dt_n.Rows[0][0].ToString();
                if (result != "")
                {
                    msg = string.Join("|", "false", ("" + (this.ShuiLiangN + 1) + "年计划未预编制,不允许此操作").ToString());
                }
                else
                {
                    int i_y = this.ShuiLiangN + 1;
                    //如果计划已经下达，则不允许差错检查
                    DataTable dt = _SpHelper.P_YSJH_SHIFOUXIADA().Tables[0];
                    int benbu = Convert.ToInt32(dt.Rows[0]["BBPLAN"].ToString());
                    int banshichu = Convert.ToInt32(dt.Rows[0]["BSCPLAN"].ToString());

                    if (benbu > 0 || banshichu > 0)
                    {
                        msg = string.Join("|", "false", ("计划已经下达，不允许差错检查!").ToString());
                    }
                    else
                    {
                        this.dtCuoWuXX = _SpHelper.P_YSJH_NIDINGJHCC(i_y).Tables[0];
                        DataTable ZJL = _YS_JH_QUEDINGJHBLL.SelectYS_JH_QUEDINGJHsDynamic(string.Format("i_type=3 and i_jlzt=0 and i_y={0}", this.ShuiLiangN + 1), null);

                        if (this.dtCuoWuXX.Rows.Count < 1)
                        {
                            msg = string.Join("|", "true", ("没有不符合规范的计划用水量").ToString(), this.dtCuoWuXX.Rows.Count.ToString(), ZJL.Rows.Count.ToString());
                        }
                        else
                        {
                            msg = string.Join("|", "succ", this.dtCuoWuXX.Rows.Count.ToString(), ZJL.Rows.Count.ToString());
                        }
                    }
                }
            }
            if (e.Parameter == "cancel")
            {
                getJHInfo();
            }
            else if (e.Parameter == "setinfo")
            {
                int V_Err = 0;
                string V_Msg;
                // 用户检查
                _SqlHelper.P_YSJH_NIDINGJHCC_BEFORE(this.ShuiLiangN + 1, txtYongHuH.Text, out V_Err, out V_Msg);
                if (V_Err > 0)
                {
                    msg = string.Join("|", "false", V_Msg);
                }
                else
                {
                    getJHInfo();

                }
            }
            else if (e.Parameter == "hfyjh")
            {
                string yhh = this.hdCollection.Get("yhh").ToString();
                int year = Convert.ToInt32(this.hdCollection.Get("year")) - 1;
                DataTable dt = _SpHelper.P_YSJH_HFYJH(year, yhh).Tables[0];
                if (dt != null && dt.Rows.Count > 0)
                {
                    msg = string.Join("|", dt.Rows[0]["I_M1"].ToString(), dt.Rows[0]["I_M2"].ToString(),
                    dt.Rows[0]["I_M3"].ToString(), dt.Rows[0]["I_M4"].ToString(), dt.Rows[0]["I_M5"].ToString(), dt.Rows[0]["I_M6"].ToString(),
                    dt.Rows[0]["I_M7"].ToString(), dt.Rows[0]["I_M8"].ToString(), dt.Rows[0]["I_M9"].ToString(), dt.Rows[0]["I_M10"].ToString(),
                    dt.Rows[0]["I_M11"].ToString(), dt.Rows[0]["I_M12"].ToString());
                }
            }
            
            e.Result = e.Parameter + "|0|" + msg;
        }

        
        /// <summary>
        /// 差错检查列表刷新
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvChaCuoJC_CustomCallback(object sender, ASPxGridViewCustomCallbackEventArgs e)
        {
            this.dtCuoWuXX = _SpHelper.P_YSJH_NIDINGJHCC(this.ShuiLiangN + 1).Tables[0];
            gvChaCuoJC.DataSource = this.dtCuoWuXX;
            gvChaCuoJC.DataBind();
        }
        #endregion

        #region 方法

        /// <summary>
        /// 初始化控件
        /// </summary>
        private void InitControl()
        {
            //根据操作人加载管理部门(站点)
            DataTable dtZhanDian = BusinessTool.GetTextAndValue("View_AS_UserInfo", "ZHANDIANMC", "ZHANDIANBH", null, string.Format("USERID={0}", LoginInfo.CurrentUser.UserID)).Tables[0];
            this.ZhanDianBH = dtZhanDian.Rows[0]["VALUE"].ToString();
        }

        
        /// <summary>
        /// 数据加载
        /// </summary>
        private void LoadData()
        {
            string water_y_m = BusinessTool.YS_GETSETTINGSVALUE(eSETTINGS_KEYNAME.WATER_Y_M.ToString());
            this.ShuiLiangN = Convert.ToInt32(water_y_m.Substring(0, 4));
            this.ShuiLiangM = Convert.ToInt32(water_y_m.Substring(4, 2));
            //txtNian2.Text = 
            txtNian.Text = (this.ShuiLiangN + 1).ToString();
        }

        /// <summary>
        /// 查错修改页签的错误记录数
        /// </summary>
        private void JiLuShu() 
        {
            DataTable CWJL = _YS_JH_NIDINGCWXXBLL.SelectYS_JH_NIDINGCWXXesAll();
            int i_cwjl = CWJL.Rows.Count;
            CuoWuJL.Text = i_cwjl.ToString();

            //DataTable ZJL = _YS_JH_QUEDINGJHBLL.SelectYS_JH_QUEDINGJHsDynamic(string.Format("i_type=3 and i_jlzt=0 and i_y={0}", this.ShuiLiangN + 1), null);
            //int i_zjl = ZJL.Rows.Count;
            ////ZongJiLu.Text = i_zjl.ToString();
        }

        /// <summary>
        ///用于控制添加、修改、查看三种状态的显示
        /// </summary>
        private void Display()
        {
            switch (this.Status)
            {
                case GridViewStatus.Init:
                    //GridViewContentTable.Visible = false;
                    break;
                case GridViewStatus.Search:
                    //btnAdd.Visible = true;
                    //GridViewContentTable.Visible = true;
                    break;
                //case GridViewStatus.Update:
                    //GridViewContentTable.Visible = true;
                   // this.gvXiangXiXX.Enabled = false;
                  //  break;
               
            }
        }

        /// <summary>
        /// 检查计划用水是否正确
        /// </summary>
        private void CheckValue() 
        {
            //保存前先做验证
            ASPxTextBox[] textBoxs = { txtM1, txtM2, txtM3, txtM4, txtM5, txtM6, txtM7, txtM8, txtM9, txtM10, txtM11, txtM12 };
            double num = 0;
            for (int i = 1; i < textBoxs.Length; i++)
            {
                int this_value = Convert.ToInt32(textBoxs[i].Text);
                int last_value = Convert.ToInt32(textBoxs[i - 1].Text);
                if (last_value == 0 || this_value == 0)
                {
                    num = 0;
                    break;
                }
                else if (last_value != 0 && this_value != 0)
                {
                    num = (this_value * 1.00) / last_value;
                    if (num < 0.2 || num > 5)
                    {
                        break;
                    }
                }
            }

            if (num >= 0.2 && num <= 5)
            {
                SaveData();
            }
            else
            {
                PageTools.ConfirmMsg(this, "计划用水量存在不合理数据，是否继续？", "btnConfirm");
            }
        }

        /// <summary>
        /// 继续保存
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        /// 
        //protected void btnConfirm_Click(object sender, EventArgs e)
        //{
        //    if (Convert.ToBoolean(this.confirmValue.Value))
        //    {
        //        SaveData();
        //    }
        //}

        /// <summary>
        /// 保存数据
        /// </summary>
        private bool SaveData()
        {
            bool result = false;
            OperateState operateState;
            operateState = OperateState.Modify;
            //将修改后的数据保存到数据库
            int i_return = _YS_JH_QUEDINGJHBLL.UpdateYS_JH_QUEDINGJHsDynamic(String.Format(@"I_M1={0},I_M2={1},
                I_M3={2},I_M4={3},I_M5={4},I_M6={5},I_M7={6},I_M8={7},I_M9={8},I_M10={9},I_M11={10},I_M12={11}",
           txtM1.Text, txtM2.Text, txtM3.Text, txtM4.Text, txtM5.Text, txtM6.Text,
                txtM7.Text, txtM8.Text, txtM9.Text, txtM10.Text, txtM11.Text, txtM12.Text),
            string.Format("S_YONGHUH='{0}' AND I_Y={1} ", txtYongHuH.Text, this.ShuiLiangN + 1));

            if (i_return > 0)
            {
                result = true;
                //_SYS_LOGBLL.Log<YS_JH_QUEDINGJH>(LoginInfo.CurrentUser, WordsStruct.I_YEWULX.计划水.ToString(), "确定计划", operateState, _YS_JH_QUEDINGJH, null);
                PageTools.AlertMsg(this, "保存成功！");
                //GridViewContentTable.Visible = false;
               // this.gvXiangXiXX.Enabled = true;
                //this.btnliulan.Visible = true;
                //数据刷新
                //this.dtCuoWuXX = _SpHelper.P_YSJH_NIDINGJHCC(this.ShuiLiangN + 1).Tables[0];
                //gvChaCuoJC.DataSource = this.dtCuoWuXX;
                //gvChaCuoJC.DataBind();
                //页数
               // CuoWuJL.Text = this.dtCuoWuXX.Rows.Count.ToString();
                //this.dtCuoWuXXXX = _SpHelper.P_YSJH_CUOWUXXLL(0, "").Tables[0];
                //gvXiangXiXX.DataSource = this.dtCuoWuXXXX;
                //gvXiangXiXX.DataBind();

            }
            return result;
        }

       

        /// <summary>
        /// 给输入框赋值
        /// </summary>
        private void SetData()
        {
            //_YS_JH_QUEDINGJH = _YS_JH_QUEDINGJHBLL.SelectYS_JH_QUEDINGJH(this.YongHuXX_ID, null);
            //txtYongHuH.Text = _YS_JH_QUEDINGJH.S_YONGHUH;
            //DataTable YONGHUXX = _YS_YG_YONGHUXXBLL.SelectYS_YG_YONGHUXXesDynamic(string.Format("S_YONGHUH='{0}'", txtYongHuH.Text), null);
            //txtYongHuM.Text = YONGHUXX.Rows[0]["S_YONGHUM"].ToString();
            //txtJuHao.Text = _YS_JH_QUEDINGJH.S_JUBIANH;
            //txtJuHao2.Text = "";
            //txtGongSiM.Text = _YS_JH_QUEDINGJH.S_GONGSIBH;
            //txtGongSiM2.Text = "";


            //if (txtGongSiM.Text != "")
            //{
            //    DataTable GongSiXX = _YS_JX_GONGSIXXBLL.SelectYS_JX_GONGSIXXesDynamic(string.Format("s_bianhao='{0}' and i_leixing=0", txtGongSiM.Text), null);
            //    txtGongSiM2.Text = GongSiXX.Rows[0][2].ToString();
            //}
            //if (txtJuHao.Text != "")
            //{
            //    DataTable JuXinX = _YS_JX_JUXINXBLL.SelectYS_JX_JUXINXesDynamic(string.Format("s_bianhao='{0}' and i_leixing=0", txtJuHao.Text), null);
            //    txtJuHao2.Text = JuXinX.Rows[0][3].ToString();
            //}

            
            ////将每一条记录1-12月计划用水量显示到下面的输入框
            //DataTable XiaDaJiHua = _YS_JH_QUEDINGJHBLL.SelectYS_JH_QUEDINGJHsDynamic(string.Format(" I_JLZT=0 and S_YONGHUH='{0}' and I_Y={1}", txtYongHuH.Text, this.ShuiLiangN + 1), null);
            //    //_YS_JH_YUANJIHUABLL.SelectYS_JH_YUANJIHUAsDynamic(string.Format(" I_JLZT=0 and S_YONGHUH='{0}' and I_Y={1}", txtYongHuH.Text, this.ShuiLiangN), null);

            //ASPxTextBox[] textBoxs = { txtM1, txtM2, txtM3, txtM4, txtM5, txtM6, txtM7, txtM8, txtM9, txtM10, txtM11, txtM12 };

            //if (XiaDaJiHua.Rows.Count > 0)
            //{
            //    if (XiaDaJiHua.Rows[0]["I_M1"].ToString()=="")
            //    {
            //        foreach (ASPxTextBox item in textBoxs)
            //        {
            //            item.Text = "0";
            //        }
            //    }
            //    else
            //    {
            //        txtM1.Text = XiaDaJiHua.Rows[0]["I_M1"].ToString();
            //        txtM2.Text = XiaDaJiHua.Rows[0]["I_M2"].ToString();
            //        txtM3.Text = XiaDaJiHua.Rows[0]["I_M3"].ToString();
            //        txtM4.Text = XiaDaJiHua.Rows[0]["I_M4"].ToString();
            //        txtM5.Text = XiaDaJiHua.Rows[0]["I_M5"].ToString();
            //        txtM6.Text = XiaDaJiHua.Rows[0]["I_M6"].ToString();
            //        txtM7.Text = XiaDaJiHua.Rows[0]["I_M7"].ToString();
            //        txtM8.Text = XiaDaJiHua.Rows[0]["I_M8"].ToString();
            //        txtM9.Text = XiaDaJiHua.Rows[0]["I_M9"].ToString();
            //        txtM10.Text = XiaDaJiHua.Rows[0]["I_M10"].ToString();
            //        txtM11.Text = XiaDaJiHua.Rows[0]["I_M11"].ToString();
            //        txtM12.Text = XiaDaJiHua.Rows[0]["I_M12"].ToString();
            //    }
                
            //}
            //else
            //{
            //    foreach (ASPxTextBox item in textBoxs)
            //    {
            //        item.Text = "0";
            //    }
            //}
            
           
            //int ZYSL = 0;
            //for (int i = 0; i < textBoxs.Length; i++)
            //{
            //    ZYSL += Convert.ToInt32(textBoxs[i].Text);
            //}
            //txtQuanNian.Text = ZYSL.ToString();
            //textBoxs[0].Focus();
        }

        /// <summary>
        /// 正则表达式验证只能输入数字或字母
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        private bool IsNatural_Number(string str)
        {
            System.Text.RegularExpressions.Regex reg1 = new System.Text.RegularExpressions.Regex(@"^[0-9]*$");
            return reg1.IsMatch(str);
        }

        protected void btnExport_Click(object sender, EventArgs e)
        {
            this.dtCuoWuXX = _SpHelper.P_YSJH_NIDINGJHCC(this.ShuiLiangN + 1).Tables[0];
            gvChaCuoJC.DataSource = this.dtCuoWuXX;
            gvChaCuoJC.DataBind();
            ASPxGridViewExporter1.GridViewID = "gvChaCuoJC";
            string sName = "错误信息";
            gvChaCuoJC.Settings.ShowFooter = false;
            ASPxGridViewExporter1.WriteXlsToResponse(sName);

        }
        #endregion



       

    }
}